<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShow = true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list" border stripe>
              <el-table-column align="center" label="序号" width="120" type="index" />
              <el-table-column align="center" label="角色名称" width="240" prop="name" />
              <el-table-column align="center" label="描述" prop="description" />
              <el-table-column align="center" label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editHandler(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delHandler(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="queryData.page"
                :page-sizes="[4, 5, 6, 7]"
                :page-size="queryData.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
    </div>
    <!--新增 / 修改 弹框 -->
    <el-dialog
      :title="title"
      :visible.sync="isShow"
      width="50%"
      @close="close"
    >
      <!-- 表单绘制 -->
      <el-form ref="form" :model="formData" :rules="formRules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="btnOK">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { getRoleListData,
  getCompanyInfo,
  delRoleData,
  getRoleInfoData,
  addRoleData,
  editRoleData } from '@/api/settings'

export default {
  name: 'Settings',
  data() {
    return {
      // 查询参数
      queryData: {
        page: 1,
        pagesize: 10
      },
      list: [], // 列表
      total: 0, // 总数
      companyData: {},
      isShow: false, // 弹框控制
      formData: { // 表单变量
        name: '',
        description: ''
      },
      formRules: { // 表单验证
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        description: [{ required: true, message: '请输入', trigger: 'blur' }]
      }

    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
    this.initData()
    this.getCompanyData()
  },
  methods: {
    // 定义获取列表方法
    async initData() {
      // 发起请求
      const { rows, total } = await getRoleListData(this.queryData)
      // 赋值
      this.list = rows
      this.total = total
    },
    // 获取公司资料
    async getCompanyData() {
      const res = await getCompanyInfo(this.$store.getters.companyId)
      this.companyData = res
    },
    handleSizeChange(val) {
      // val是每页最新数量
      this.queryData.pagesize = val
      this.initData()
    },
    handleCurrentChange(val) {
      // val是每页最新的页码
      this.queryData.page = val
      this.initData()
    },
    // 删除事件
    async delHandler(val) {
      // console.log(val)
      await this.$confirm('确认删除吗？')
      // console.log(111)
      await delRoleData(val.id)
      this.$message.success('删除成功')
      // 如果不是第一页， 且改业只有一个元素 且删除成功： 页码-1 获取数据
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initData()
    },
    // 修改点击事件，获取修改行数据
    async editHandler(row) {
      console.log(row)
      const res = await getRoleInfoData(row.id)
      this.formData = res
      this.isShow = true
    },

    // 确定提交事件
    async btnOK() {
    // 1.获取校验结果
      await this.$refs.form.validate()
      console.log(111)
      // 2.发送请求
      if (this.formData.id) {
      // 修改
        await editRoleData(this.formData)
      } else {
      // 添加
        await addRoleData(this.formData)
      }
      // 3. 提示成功
      this.$message.success('操作成功')
      // 4.列表更新
      this.initData()
      // 5.关闭
      this.isShow = false
    },
    // 关闭
    close() {
    //  重置表单
      this.formData = {}
      // 移除校验
      this.$refs.form.clearValidate()
    }
  }
}
</script>
